import random
from utils.proxy import Proxy_IP
import requests
from retry import retry
from Configs.setting import logger, waiwang_proxies_list
import my_fake_useragent as ua1

from utils.retry_model2 import MyRetry

ua = ua1.UserAgent(family="chrome").random()
requests.packages.urllib3.disable_warnings()


class Request2:

    def __init__(self):
        self.headers = {
            "user-agent": str(ua)
        }

    @retry(tries=10, delay=1)
    def GetResponse(self, url, S=None, method="GET", headers=None, is_proxy=None, params=None, data=None, json=None,file=None,
                    timeout: int = 30, verify=False, allow_redirects=True):
        try:
            if S == None:
                S = requests.Session()
                S.headers = {
                    "user-agent": str(ua)
                }

            if headers:
                S.headers = headers

            if not is_proxy:
                S.proxies = Proxy_IP.get_proxy()
            elif is_proxy == 'waiwang':
                S.proxies = random.choice(waiwang_proxies_list)
            elif is_proxy == 'wu':
                pass
            elif is_proxy == 'rs':
                pass
            else:
                S.proxies = is_proxy

            if method == "GET":
                if params == None:
                    response = S.get(url, timeout=timeout, allow_redirects=allow_redirects, verify=verify)
                else:
                    response = S.get(url, timeout=timeout, params=params, allow_redirects=allow_redirects,
                                     verify=verify)
            elif method == "POST":
                if data != None:
                    response = S.post(url, timeout=timeout, data=data, allow_redirects=allow_redirects, verify=verify,files=file)
                elif json != None:
                    response = S.post(url, timeout=timeout, json=json, allow_redirects=allow_redirects, verify=verify,files=file)
                else:
                    response = S.post(url, timeout=timeout, allow_redirects=allow_redirects, verify=verify,files=file)
            else:
                return f"暂未支持该模式：{method},请联系作者处理！"
            if response.status_code in [403, 502]:
                raise ValueError('状态码在403,502')
            if is_proxy == 'rs':
                return response, S
            return response
        except Exception as e:
            logger.warning(e)
            raise


    @retry(tries=5, delay=1)
    def GetResponse3(self,url,S,timeout: int = 20,return_S=True,data=None,headers = None,params = None,is_proxy=None,method="GET",allow_redirects=True,cookies=None):
        try:
            if not headers:
                S.headers = {
                    "user-agent": str(ua)
                }
            if not is_proxy:
                S.proxies = Proxy_IP.get_proxy()
            elif is_proxy == 'no':
                pass

            if method == "GET":
                response = S.get(url, timeout=timeout, data=data,params=params,allow_redirects = allow_redirects,cookies=cookies)
            elif method == "POST":
                response = S.post(url, timeout=timeout, data=data,params=params,allow_redirects = allow_redirects,cookies=cookies)
            else:
                if return_S:
                    return f"暂未支持该模式：{method},请联系作者处理！", ''
                return f"暂未支持该模式：{method},请联系作者处理！"
            if return_S:
                return response, S
            return response
        except Exception as e:
            raise

    @staticmethod
    @MyRetry(times=3, custom_return=(True, False))
    def get(url, headers={}, params={}, cookies={}, proxies={}, timeout=(3, 5), auth=None, session=None):
        if session:
            return session.get(url, headers=headers, params=params, cookies=cookies, proxies=proxies, timeout=timeout, auth=auth)
        else:
            return requests.get(url, headers=headers, params=params, cookies=cookies, proxies=proxies, timeout=timeout, auth=auth)

    @staticmethod
    @MyRetry(times=3, custom_return=(True, False))
    def post(url, headers={}, data={}, json={}, cookies={}, proxies={}, timeout=(3, 5), auth=None, session=None):
        if session:
            return session.post(url, headers=headers, data=data, json=json, cookies=cookies, proxies=proxies, timeout=timeout, auth=auth)
        else:
            return requests.post(url, headers=headers, data=data, json=json, cookies=cookies, proxies=proxies, timeout=timeout, auth=auth)


Request2 = Request2()
